<?php
/**
 * Authenticate check for admin area
 *
 * <code>
 *  $username = "user";
 *  $realm = "desktop";
 *  $password = md5('password');
 *  $passwordForFile = md5("$username:$realm:$password");
 * </code>
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */

class Rockitbos_LoginController extends Zend_Controller_Action
{
    /**
     * Initialize the general options
     *
     * @return void
     */
    public function init()
    {
        $this->_helper->layout->disableLayout();
    }
    /**
     * Index
     *
     * @return void
     */
    public function indexAction()
    {
    }
    /**
     * Login form
     *
     * @return void
     */
    public function loginAction()
    {
       $form = new Rockitbos_Form_Login;

       $buildForm = $form->build();
       $username = $this->getRequest()->getPost('username');
       $password = md5($this->getRequest()->getPost('password'));

       if($username && $password) {

           if ($form->isValid($_POST)) {

               if ($this->_isValid($username, $password)) {

                   $this->_forward('index', 'dashboard');
               }
           }
       }
       $this->view->form = $buildForm;

    }
    /**
     * Authenticate check
     *
     * @param string &$username
     * @param string &$password
     *
     * @return boolean
     */
    protected function _isValid(&$username, &$password)
    {
        $mandant = 'default';

        $filename = 'home/default/rockitbos/userlist.dat';
        $realm = 'desktop';

        $adapter = new Zend_Auth_Adapter_Digest(
            $filename,
            $realm,
            $username,
            $password);

        $result = $adapter->authenticate();

        if($result->isValid()) {

            $auth = Zend_Auth::getInstance();
            $auth->getStorage()->write($result->getIdentity());
            return TRUE;
        }
        return FALSE;
    }
}